<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>案例_用户登录.html</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
  <style>
    html,
    body {
      background-color: #f8f8f8;
    }

    .login-box {
      width: 400px;
      position: fixed;
      top: 20%;
      left: 50%;
      transform: translateX(-50%);
      border: 1px solid #efefef;
      padding: 20px;
      border-radius: 4px;
      box-shadow: 1px 1px 5px #cfcfcf;
      background-color: #fff;
      transition: box-shadow 0.3s ease;
    }

    .login-box:hover {
      transition: box-shadow 0.3s ease;
      box-shadow: 1px 1px 20px #cfcfcf;
    }
  </style>
</head>

<body>
  <div class="login-box">
    <form id="myForm">
      <div class="form-group">
        <label for="username">用户名</label>
        <!-- 账号 -->
        <input type="text" name="username" class="form-control" id="username" autocomplete="off">
        <small id="emailHelp" class="form-text text-muted">
          The available account is<strong>admin</strong>
        </small>
      </div>
      <div class="form-group">
        <!-- 密码 -->
        <label for="password">密码</label>
        <input type="password" class="form-control" id="password" name="password">
        <small id="emailHelp" class="form-text text-muted">The available password is
          <strong>123456</strong></small>
      </div>
      <button type="submit" class="btn btn-primary" id="btnLogin">Submit</button>
    </form>
  </div>

  <script src="https://cdn.jsdelivr.net/npm/axios@0.27.2/dist/axios.min.js"></script>
  <script src="./lib/form-serialize.js"></script>
  <script>
    document.querySelector('#btnLogin').addEventListener('click', (e) => {
      e.preventDefault()
       // 获取用户名和密码
        const username = document.querySelector('#username').value
        const password = document.querySelector('#password').value
        
        console.log(username, password)

        // Content-Type: application/json
        axios({
            url: 'http://ajax-api.itheima.net/api/login',
            method: 'POST',
            data: {
                username: username,
                password: password
            }
        })

        // Content-Type: application/x-www-form-urlencoded
        // const result = serialize(document.querySelector('#myForm'))
        // // const result = serialize(document.querySelector('#myForm'), {hash: true})
        // console.log(result)
        // axios({
        //     url: 'http://ajax-api.itheima.net/api/login',
        //     method: 'POST',
        //     data: result
        // })

        // Content-Type: multipart/form-data;
        // const fd = new FormData()
        // fd.append('username', username)
        // fd.append('password', password)
        // axios({
        //     url: 'http://ajax-api.itheima.net/api/login',
        //     method: 'POST',
        //     data: fd
        // })

    })
  </script>
</body>

</html>